<?php

include 'init.php';

$type=trim($_POST['t']);

ob_clean();

if($type=='ckmail')
{
	$data=trim($_POST['d']);
	$usr=new userModel();
	if($usr->hasuser('email',$data))
	{
		echo json_encode('had');
	}
	else
	{
		echo json_encode('no');
	}
	exit;
}
else if($type=='regin')
{
	//60秒内不能发送第二次邮件
	//如果时间在90秒内，返回
	session_start();
	if(isset($_SESSION['mailtime'])&&(time()-$_SESSION['mailtime'])<60)
	{
		echo json_encode('forbiden');
		exit;
	}

	$valcode=trim($_POST['validatecode']);
	//检查验证码
	
	if(isset($_SESSION['valcode']))
	{
		if($valcode!=$_SESSION['valcode'])
		{
			echo json_encode('code');
			exit;
		}
	}
	else
	{
		echo json_encode('code');
		exit;
	}
	//session_destroy();


	$data=array();

	$data['name']=trim($_POST['nickname']);
	$data['pwd']=trim($_POST['pwd']);
	$repwd=trim($_POST['repwd']);
	$data['sex']=trim($_POST['sex']);

	$year=trim($_POST['year']);
	$month=trim($_POST['month']);
	$day=isset($_POST['day'])?trim($_POST['day']):1;

	$country=trim($_POST['country'])==''||trim($_POST['country'])=='中国'?'':trim($_POST['country']).' ';
	$state=isset($_POST['province'])&&trim($_POST['province'])!=''?trim($_POST['province']).' ':'';
	$city=trim($_POST['city']);
	
	$data['livetown']=$country.$state.$city;

	$data['email']=trim($_POST['email']);
	

	//检查用户名
	if($data['name']=="")
	{
		echo json_encode('nick');
		exit;
	}
	//检查密码
	if($data['pwd']==""||strlen($data['pwd'])<6||strlen($data['pwd'])>16)
	{
		echo json_encode('pwd');
		exit;
	}
	//检查重输密码
	if($repwd==""||$repwd!=$data['pwd'])
	{
		echo json_encode('repwd');
		exit;
	}

	//检查email
	if($data['email']==""||!preg_match("/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/",$data['email']))
	{
		echo json_encode('email');
		exit;
	}
	
	//计算时间戳
	$data['birthday']=strtotime("$year-$month-$day");
	
	//print_r($data);
	//exit;
	//入库
	$usr=new userModel();
	$id=$usr->add($data);
	if($id)//暂时使用方案1
	{
		/*
			方案1 account字段不递增,手动设置，以防止普通用户使用靓号码
		*/
		
		//设置account
		$Acc=new accountModel();
		$new_account=$usr->getMax('account')+1;
		//如果在靓号码库有此号码，则加1，直到号码库没有为止
		while($Acc->hasAcc($new_account)>0)
		{
			$new_account+=1;
		}
		//更新数据库
		if(!$usr->modify($id,'account',$new_account))
		{
			echo json_encode('add');
			exit;
		}
		

		/*
			方案2 account 字段递增,数据库自动设置后再取出使用,但不能控制靓号使用
		*/
		//$new_account=$usr->getAttr($id,'account');


		//创建一个邮件检验session
		//session_start();
		$code=time().','.md5($data['email']).','.md5($data['name']);
		$_SESSION['valmailcode']=$code;
		$_SESSION['usr']=$id;
		$_SESSION['email']=$data['email'];
		
		ini_set('SMTP','smtp.163.com');
		ini_set('smtp_port','25');

		require (ROOT.'tools/class.phpmailer.php');
		$mailer=new PHPMailer();
		$mailer->Charset='utf-8';
		$mailer->ContentType='text/html';
		$mailer->Encoding='base64';
		$mailer->SetLanguage('zh_cn');
		$mailer->IsSMTP();
		$mailer->Host='smtp.163.com';
		$mailer->Port=25;
		$mailer->SMTPAuth=true;
		$mailer->Username='13145768107@163.com';
		$mailer->Password='mail163';
		$mailer->FromName='肥扣扣在线聊天网';
		$mailer->From='13145768107@163.com';
		$mailer->AddAddress($data['email'],$data['name']);
		$mailer->Subject='邮箱验证';
		$cont="<center>
		<h2>欢迎注册肥扣扣,请您在收到邮件30分钟内点击如下链接完成邮箱验证，过期无效</h2>
		<h3><a href='http://fakeqq.win/action/validatemail.php?code=$code'>点击验证邮箱>>></a></h3>
		<font color='red'>*如果您没有注册，请不要理会*</font>
		</center>
									<div style='float:right;'>--肥扣扣在线</div>
		";
		$mailer->Body=$cont;
		if($mailer->Send())
		{
			//echo '重置密码连接已经发到您的邮箱，请在30分钟内查收，过期无效';

			//重设限制时间值
			$_SESSION['mailtime']=time();
			//返回新的account
			echo json_encode('ok_'.$id.'_'.$new_account);
			exit;
		}
		else
		{
			//echo '邮件发送失败，请重试';
			echo json_encode('mailerr_'.$id.'_'.$new_account);
			exit;
		}
		
		

	}
	else
	{
		echo json_encode('add');
		exit;
	}





	//注册完成后销毁sesssion
	//session_destroy();
}
else if($type=='resendmail')
{
	//60秒内不能发送第二次邮件
	//如果时间在90秒内，返回
	session_start();
	if(isset($_SESSION['mailtime'])&&(time()-$_SESSION['mailtime'])<60)
	{
		echo json_encode('forbiden');
		exit;
	}

	$id=trim($_POST['id']);
	$email=trim($_POST['email']);
	$name=trim($_POST['name']);
	if($id==''||$email==''||$name=='')
	{
		//id参数错误
		echo json_encode('attrerr');
		exit;
	}

	$usr=new userModel();
	//检查是否已经通过验证
	
	$user=$usr->getbyid($id);

	if($user)
	{
		if($user['email_validate']=='1')
		{
			//已经注册
			echo json_encode('isvali');
			exit;
		}

		
		//创建一个邮件检验session
		//session_start();
		$code=time().','.md5($email).','.md5($name);
		$_SESSION['valmailcode']=$code;
		$_SESSION['usr']=$id;
		$_SESSION['email']=$email;
		ini_set('SMTP','smtp.163.com');
		ini_set('smtp_port','25');

		require (ROOT.'tools/class.phpmailer.php');
		$mailer=new PHPMailer();
		$mailer->Charset='utf-8';
		$mailer->ContentType='text/html';
		$mailer->Encoding='base64';
		$mailer->SetLanguage('zh_cn');
		$mailer->IsSMTP();
		$mailer->Host='smtp.163.com';
		$mailer->Port=25;
		$mailer->SMTPAuth=true;
		$mailer->Username='13145768107@163.com';
		$mailer->Password='mail163';
		$mailer->FromName='肥扣扣在线聊天网';
		$mailer->From='13145768107@163.com';
		$mailer->AddAddress($email,$name);
		$mailer->Subject='邮箱验证';
		$cont="<center>
		<h2>欢迎注册肥扣扣,请您在收到邮件30分钟内点击如下链接完成邮箱验证，过期无效</h2>
		<h3><a href='http://fakeqq.win/action/validatemail.php?code=$code'>点击验证邮箱>>></a></h3>
		<font color='red'>*如果您没有注册，请不要理会*</font>
		</center>
									<div style='float:right;'>--肥扣扣在线</div>
		";
		$mailer->Body=$cont;
		if($mailer->Send())
		{
			//重设限制时间值
			$_SESSION['mailtime']=time();
			echo json_encode('ok');
			exit;
		}
		else
		{
			echo json_encode('mailerr');
			exit;
		}





	}
	else
	{
		//没有此用户
		echo json_encode('nousr');
		exit;
	}

}